package com.atguigu.srb.sms.controller.api;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.util.RegexValidateUtils;
import com.atguigu.srb.sms.client.CoreUserInfoClient;
import com.atguigu.srb.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zwj
 * @create 2021-10-11 23:37
 */
@RestController
@Slf4j
//@CrossOrigin
@RequestMapping("/api/sms")
@Api(tags = "短信服务管理")
public class ApiSmsController {
    @Autowired
    private SmsService smsService;
    @Autowired
    private CoreUserInfoClient coreUserInfoClient;

    @GetMapping("/send/{mobile}")
    @ApiOperation("发送短信验证码")
    public R send(
            @ApiParam(value = "手机号码",required = true)
            @PathVariable("mobile") String mobile){
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);

        boolean result = coreUserInfoClient.checkMobile(mobile);
        Assert.isTrue(result==false,ResponseEnum.MOBILE_EXIST_ERROR);
        smsService.send(mobile);
        return R.ok().message("短信发送成功");
    }
}
